// src/shared/api/cart.ts

import { ApiResponse } from '../types';
import {
  getCart as getCartService,
  addToCart as addToCartService,
  updateCartItem as updateCartItemService,
  removeFromCart as removeFromCartService,
  clearCart as clearCartService,
  updateCartItemsSelected as updateCartItemsSelectedService,
  CartListResponse,
  AddToCartRequest,
  CartItemResponse,
  UpdateCartItemRequest
} from '../service/cartService';

/**
 * Mock API: 获取购物车列表
 * GET /api/cart
 */
export const GET_cart = async (): Promise<ApiResponse<CartListResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 300));

    // 调用Service层
    const result = await getCartService();

    return result;
  } catch (error) {
    console.error('Get Cart API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 添加商品到购物车
 * POST /api/cart
 */
export const POST_cart = async (body: AddToCartRequest): Promise<ApiResponse<CartItemResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 400));

    // 调用Service层
    const result = await addToCartService(body);

    return result;
  } catch (error) {
    console.error('Add To Cart API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 更新购物车项
 * PUT /api/cart/:id
 */
export const PUT_cart_item = async (cartItemId: string, body: UpdateCartItemRequest): Promise<ApiResponse<CartItemResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 350));

    // 调用Service层
    const result = await updateCartItemService(cartItemId, body);

    return result;
  } catch (error) {
    console.error('Update Cart Item API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 删除购物车项
 * DELETE /api/cart/:id
 */
export const DELETE_cart_item = async (cartItemId: string): Promise<ApiResponse<null>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 300));

    // 调用Service层
    const result = await removeFromCartService(cartItemId);

    return result;
  } catch (error) {
    console.error('Remove From Cart API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 清空购物车
 * DELETE /api/cart
 */
export const DELETE_cart = async (): Promise<ApiResponse<null>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 400));

    // 调用Service层
    const result = await clearCartService();

    return result;
  } catch (error) {
    console.error('Clear Cart API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 批量更新购物车项选中状态
 * PUT /api/cart/selected
 */
export interface UpdateCartSelectedRequest {
  cartItemIds: string[];
  selected: boolean;
}

export const PUT_cart_selected = async (body: UpdateCartSelectedRequest): Promise<ApiResponse<null>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 350));

    // 调用Service层
    const result = await updateCartItemsSelectedService(body.cartItemIds, body.selected);

    return result;
  } catch (error) {
    console.error('Update Cart Selected API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};